package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/2 4:04 PM
 */
public class LC_946 {

    public static void main(String[] args) {
        System.out.println(
                new LC_946().validateStackSequences(
                        new int[]{1, 2, 3, 4, 5},
                        new int[]{4, 3, 5, 2, 1}
                )
        );
    }

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed.length != popped.length) {
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int i1 = 0;
        for (int i = 0; i < popped.length; i++) {
            if (!stack.empty()) {
                if (popped[i] == stack.peek()) {
                    stack.pop();
                    continue;
                }
            }
            pop:
            {
                for (; i1 < pushed.length; i1++) {
                    if (pushed[i1] != popped[i]) {
                        stack.push(pushed[i1]);
                    } else {
                        i1++;
                        break pop;
                    }
                }
                return false;
            }

        }

        return true;
    }
}
